//
//  YYTCaptureView.m
//  CameraDemo
//
//  Created by apple on 2017/3/9.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "YYTCaptureView.h"

@interface YYTCaptureView ()

@end
@implementation YYTCaptureView
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initCapture];
    }
    return self;
}
/**
 初始化摄像头
 */
- (void)initCapture {
    //1.创建媒体管理会话
    AVCaptureSession *session = [[AVCaptureSession alloc]init];
    self.session = session;
    //判断分辨率是否支持1280*720 支持就设置为1280*720
    if ([session canSetSessionPreset:AVCaptureSessionPreset1280x720]) {
        session.sessionPreset = AVCaptureSessionPreset1280x720;
    }
    //2.获取后置摄像头设备对象
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    //3.创建输入数据对象
    NSError *error = nil;
    AVCaptureDeviceInput *captureInput = [[AVCaptureDeviceInput alloc]initWithDevice:device error:&error];
    if (error) {
        NSLog(@"创建输入数据对象错误");
        return;
    }
    self.captureInput = captureInput;
    //4.创建输出数据对象
    //    AVCaptureStillImageOutput *imgOutput = [[AVCaptureStillImageOutput alloc]init];
    //    NSDictionary *setting = @{AVVideoCodecKey : AVVideoCodecJPEG};
    //    [imgOutput setOutputSettings:setting];
    //    self.imageOutput = imgOutput;
    //5.添加输入数据对象和输出对象到会话中
    if ([session canAddInput:captureInput]) {
        [session addInput:captureInput];
    }
    //    if ([session canAddOutput:imgOutput]) {
    //        [session addOutput:imgOutput];
    //    }
    //6.创建视频预览图层
    AVCaptureVideoPreviewLayer *videoLayer = [[AVCaptureVideoPreviewLayer alloc]initWithSession:session];
    self.layer.masksToBounds = YES;
    videoLayer.frame = self.bounds;
    videoLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    self.captureLayer = videoLayer;
}

@end
